package com.yjhb.android.sbz.tool;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;

import com.bumptech.glide.Glide;
import com.yjhb.android.sbz.App;
import com.yjhb.android.sbz.R;
import com.jojo.android.zxlib.interfaces.Callback;
import com.jojo.android.zxlib.tool.ZxQrCode;
import com.jojo.android.zxlib.tool.ZxTool;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * 创建者 zx
 * 创建时间 2021/1/6 10:25
 */
public class PosterFactory {
    private final String TAG = "PosterFactory";
    public static final String[] themeUrls = {
            "https://shangbangzhuan.oss-cn-beijing.aliyuncs.com/commonImages/qrcode_theme1.png",
            "https://shangbangzhuan.oss-cn-beijing.aliyuncs.com/commonImages/qrcode_theme2.png",
            "https://shangbangzhuan.oss-cn-beijing.aliyuncs.com/commonImages/qrcode_theme3.png"
    };

//    public static final int[] ids = {R.mipmap.qrcode_theme1, R.mipmap.qrcode_theme2, R.mipmap.qrcode_theme3};

    public static Bitmap Poster(Context activity, String url, int themeIndex) throws Exception {
        Bitmap bitmap = null;
        switch (themeIndex) {
            case 0:
                bitmap = Theme1(activity, url);
                break;
            case 1:
                bitmap = Theme2(activity, url);
                break;
            case 2:
                bitmap = Theme3(activity, url);
                break;
        }
        return bitmap;
    }

    /**
     * 异步生成海报
     *
     * @param activity
     * @param url
     * @param themeIndex
     * @param callback
     */
    public static void AsyncPoster(Context activity, String url, int themeIndex, Callback<Bitmap> callback) {
        Observable.create(new ObservableOnSubscribe<Bitmap>() {
            @Override
            public void subscribe(ObservableEmitter<Bitmap> e) throws Exception {
                e.onNext(PosterFactory.Poster(activity, url, themeIndex));
            }
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Bitmap>() {
                    @Override
                    public void accept(Bitmap bitmap) throws Exception {
                        callback.callback(bitmap);
                    }
                });
    }


    private static Bitmap Theme1(Context context, String url) throws Exception {
        Paint paint = new Paint();
        Canvas canvas = null;
        Bitmap resource = Glide.with(context).asBitmap().load(R.mipmap.logo).submit().get();
        //主题一
        Bitmap qrBitmap = ZxQrCode.generateQRCode(url, resource,
                600, 600);
        Bitmap tmp = Glide.with(context).asBitmap().load(themeUrls[0]).submit().get();
        paint.setTextAlign(Paint.Align.CENTER);
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(context.getResources().getColor(R.color.red_48));
        paint.setAntiAlias(true);
        paint.setStrokeWidth(10);
        paint.setTypeface(Typeface.DEFAULT_BOLD);
        paint.setTextSize(60);
        Bitmap bitmap;
        canvas = new Canvas(bitmap = Bitmap.createBitmap(tmp.getWidth(), tmp.getHeight(), Bitmap.Config.ARGB_8888));
        canvas.drawBitmap(tmp, 0, 0, paint);
        canvas.drawText("邀请码:" + App.userInfo.getId(),
                bitmap.getWidth() / 2, 3 * 235, paint
        );
        paint.setTypeface(Typeface.DEFAULT);
        paint.setTextSize(39);
        canvas.drawText("新人扫码下载赏帮APP赢更多好礼",
                bitmap.getWidth() / 2, 3 * 260, paint
        );
        canvas.drawBitmap(qrBitmap, new Rect(0, 0, qrBitmap.getWidth(), qrBitmap.getHeight()), new Rect(bitmap.getWidth() / 2 - (qrBitmap.getWidth() / 2),
                3 * 310,
                bitmap.getWidth() / 2 + (qrBitmap.getWidth() / 2),
                3 * 310 + qrBitmap.getHeight()), paint);

        paint.setTextSize(ZxTool.sp2px(14));
        canvas.drawText("（注：请使用浏览器扫码打开）",
                bitmap.getWidth() / 2, 3 * 310 + qrBitmap.getHeight() + 3 * 25, paint
        );
        return bitmap;

    }

    private static Bitmap Theme2(Context context, String url) throws Exception {
        Paint paint = new Paint();
        Canvas canvas = null;
        Bitmap resource = Glide.with(context).asBitmap().load(R.mipmap.logo).submit().get();
        Bitmap qrBitmap = ZxQrCode.generateQRCode(url, resource,
                450, 450);
        Bitmap tmp = Glide.with(context).asBitmap().load(themeUrls[1]).submit().get();
        paint.setTextAlign(Paint.Align.CENTER);
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(context.getResources().getColor(R.color.red_48));
        paint.setAntiAlias(true);
        paint.setStrokeWidth(10);
        paint.setTypeface(Typeface.DEFAULT_BOLD);
        paint.setTextSize(60);
        Bitmap bitmap;
        canvas = new Canvas(bitmap = Bitmap.createBitmap(tmp.getWidth(), tmp.getHeight(), Bitmap.Config.ARGB_8888));
        canvas.drawBitmap(tmp, 0, 0, paint);
        canvas.drawBitmap(qrBitmap, new Rect(0, 0, qrBitmap.getWidth(), qrBitmap.getHeight()), new Rect(bitmap.getWidth() / 2 - (qrBitmap.getWidth() / 2),
                3 * 395,
                bitmap.getWidth() / 2 + (qrBitmap.getWidth() / 2),
                3 * 395 + qrBitmap.getHeight()), paint);
        paint.setColor(context.getResources().getColor(R.color.purple_f9));
        paint.setTypeface(Typeface.DEFAULT_BOLD);
        paint.setTextSize(60);
        canvas.drawText("邀请码:" + App.userInfo.getId(),
                bitmap.getWidth() / 2, 3 * 420 + qrBitmap.getHeight(), paint
        );
        paint.setTypeface(Typeface.DEFAULT);
        paint.setTextSize(42);
        canvas.drawText("（注：请使用浏览器扫码打开）",
                bitmap.getWidth() / 2, 3 * 445 + qrBitmap.getHeight(), paint
        );
        return bitmap;

    }

    private static Bitmap Theme3(Context context, String url) throws Exception {
        Paint paint = new Paint();
        Canvas canvas = null;
        Bitmap resource = Glide.with(context).asBitmap().load(R.mipmap.logo).submit().get();
        Bitmap qrBitmap = ZxQrCode.generateQRCode(url, resource,
                145*3, 145*3);
        Bitmap tmp = Glide.with(context).asBitmap().load(themeUrls[2]).submit().get();
        paint.setTextAlign(Paint.Align.CENTER);
        paint.setStyle(Paint.Style.FILL);
        paint.setAntiAlias(true);
        paint.setStrokeWidth(10);
        Bitmap bitmap;

        paint.setColor(context.getResources().getColor(R.color.black));
        paint.setTypeface(Typeface.DEFAULT_BOLD);
        paint.setTextSize(60);
        canvas = new Canvas(bitmap = Bitmap.createBitmap(tmp.getWidth(), tmp.getHeight(), Bitmap.Config.ARGB_8888));
        canvas.drawBitmap(tmp, 0, 0, paint);
        canvas.drawText("邀请码:" + App.userInfo.getId(),
                bitmap.getWidth() / 2, 3 * 370, paint
        );
        canvas.drawBitmap(qrBitmap, new Rect(0, 0, qrBitmap.getWidth(), qrBitmap.getHeight()), new Rect(bitmap.getWidth() / 2 - (qrBitmap.getWidth() / 2),
                3 * 380,
                bitmap.getWidth() / 2 + (qrBitmap.getWidth() / 2),
                3 * 380 + qrBitmap.getHeight()), paint);

        paint.setTypeface(Typeface.DEFAULT);
        paint.setTextSize(45);
        canvas.drawText("（注：请使用浏览器扫码打开）",
                bitmap.getWidth() / 2, canvas.getHeight() - 3 * 35, paint
        );
        return bitmap;

    }

}
